package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.RoleMenuMapper;
import com.gxa.zhonglong.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName RoleMenuService
 * @Author: ws
 * @date 2021/10/27 9:16
 */
@Service("RoleMenuService")
@Transactional(rollbackFor = {Throwable.class})
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<List<Object>> getList(Long roleId) {

        // 最终返回list
        List<List<Object>> endList = new ArrayList<>();

        List<Object> list = new ArrayList<>();

        // firstList：一级权限列表
        List<Map<String, Object>> firstList = roleMenuMapper.getFirstList(roleId);

        // 处理二级权限列表，体现与一级的层级关系(二级list2)
        // menuId：用于查询对应二级权限
        // secondList：装每次得到的二级权限列表
        Long menuId = null;
        Long menuId2 = null;
        List<Map<String, Object>> secondList = null;
        for (Map<String, Object> map : firstList) {
            menuId = (Long) map.get("value");
            list.add(menuId);
            secondList = roleMenuMapper.getSecondList(roleId, menuId);
            if (secondList.size() > 0) {
                for (Map<String, Object> stringMap : secondList) {
                    menuId2 = (Long) stringMap.get("value");
                    list.add(menuId2);
                }
            }
            endList.add(list);
            list = new ArrayList<>();
        }


        return endList;
    }

    @Override
    public List<Map<String, Object>> getMenuList() {
        List<Map<String, Object>> list = roleMenuMapper.getAllFirst();
        Long id = null;
        List<Map<String, Object>> allSecond = null;
        for (Map<String, Object> map : list) {
            id = (Long) map.get("value");
            allSecond = roleMenuMapper.getAllSecond(id);
            map.put("children", allSecond);
        }
        return list;
    }

    @Override
    public void update(Long roleId, List menuIds) throws BusinessException {

        // 判断传入的数组是否有数据
        if (menuIds == null || menuIds.size()==0) {
            roleMenuMapper.delete(roleId);
        } else {

            // 删除对应角色的所有权限
            roleMenuMapper.delete(roleId);
            // 添加对应角色新的权限
            // 遍历menuIds数组，批量添加
            Integer add = null;
            Long[] ids = new Long[menuIds.size()];
            for (int i = 0; i < menuIds.size(); i++) {
                ids[i] = Long.parseLong(menuIds.get(i).toString()) ;
                add = roleMenuMapper.add(roleId, ids[i]);
                if (add == 0) {
                    throw new BusinessException(ExceptionCode.UPDATE_FALSE);
                }
            }

        }
    }


}
